package com.tksflysun.im.api.controller;

import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.tksflysun.im.api.bean.LoginReq;
import com.tksflysun.im.api.qo.UserQo;
import com.tksflysun.im.api.redis.VerifyCodeRedisCache;
import com.tksflysun.im.api.service.LoginLogService;
import com.tksflysun.im.api.service.UserService;
import com.tksflysun.im.common.bean.LoginLog;
import com.tksflysun.im.common.bean.User;
import com.tksflysun.im.common.constants.Constants;
import com.tksflysun.im.common.result.ControllerResult;
import com.tksflysun.im.common.util.DateUtil;
import com.tksflysun.im.common.util.Md5Util;
import com.tksflysun.im.common.util.StringUtil;

@RestController
@RequestMapping("/authentication")
public class AuthenticationController {
    @Autowired
    private UserService userService;
    @Autowired
    private LoginLogService loginLogService;

    @RequestMapping(path = "/verifycode", method = RequestMethod.POST)

    public ControllerResult getVerifyCode(@RequestBody LoginReq loginReq) {
        ControllerResult result = ControllerResult.getSuccessResult();
        String verifyCode = StringUtil.getRandom(6);
        result.setData(verifyCode);
        VerifyCodeRedisCache.getInstance().setexT(loginReq.getMobilePhone(), verifyCode.toLowerCase());
        return result;
    }

    @RequestMapping(path = "/name/login", method = RequestMethod.POST)
    public ControllerResult nameLogin(@RequestBody LoginReq loginReq) {
        ControllerResult result = ControllerResult.getSuccessResult();
        UserQo userQo = new UserQo();
        userQo.setUserName(loginReq.getUserName());
        User user = userService.getUserWihtPassword(userQo);
        if (user.getPassword().equals(Md5Util.getMd5Str(loginReq.getPassword() + user.getSalt()))) {
            String token = UUID.randomUUID().toString().replace("-", "");
            result.setData(token);
            user.setPassword(null);
            user.setSalt(null);
            LoginLog loginLog = new LoginLog();
            loginLog.setUserId(user.getUserId());
            loginLog.setLoginTimeStamp(DateUtil.getTimeStampStr());
            loginLog.setStatus(Constants.LOGIN_STATUS.ACTIVE);
            loginLog.setToken(token);
            loginLogService.login(loginLog);
            user = null;
            return result;
        }
        result.setCode(ControllerResult.LOGIN_FAIL);
        return result;
    }

    @RequestMapping(path = "/phone/login", method = RequestMethod.POST)
    public ControllerResult phoneLogin(@RequestBody LoginReq loginReq) {
        ControllerResult result = ControllerResult.getSuccessResult();

        String existsVerifyCode = VerifyCodeRedisCache.getInstance().getT(loginReq.getMobilePhone(), String.class);
        if (!loginReq.getVerifyCode().equals(existsVerifyCode)) {
            result.setCode(ControllerResult.VERIFY_CODE_OUT);
            String token = UUID.randomUUID().toString().replace("-", "");
            result.setData(token);
            return result;
        }
        result.setCode(ControllerResult.LOGIN_FAIL);
        return result;
    }
}
